/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pantallaConsultar.java
 *
 * Created on 06-ago-2010, 15:44:11
 */

package vista.rrhh;

import controlador.rrhh.gestorConsultarLicenciasEmpleado;
import controlador.rrhh.gestorGestionarLicenciasEmpleado;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import util.NTupla;
import util.SwingPanel;
import util.TablaUtil;
import vista.*;
import vista.interfaces.ICallBack;

/**
 *
 * @author Administrador
 */
public class pantallaConsultarLicenciasEmpleado extends javax.swing.JInternalFrame implements ICallBack {

    private gestorConsultarLicenciasEmpleado gestor;
    private int ID_EMPLEADO = 0;

    /** Creates new form pantallaConsultar */
    public pantallaConsultarLicenciasEmpleado() {
        initComponents();

        gestor = new gestorConsultarLicenciasEmpleado(this);

        habilitarVentana();
        this.setTitle("Consultar Licencias");

    }

    private void habilitarVentana()
    {

        mostrarLicencias();

    }

    private void refrescarTabla()
    {
        mostrarLicencias();
    }

    private void mostrarLicencias()
    {
        DefaultTableModel modelo = (DefaultTableModel) tblLista.getModel();

        modelo = TablaUtil.vaciarDefaultTableModel(modelo);

        ArrayList<NTupla> lista = gestor.mostrarLicenciasActivas(this.ID_EMPLEADO);
        Iterator it = lista.iterator();
        while (it.hasNext())
        {
            NTupla nt = (NTupla) it.next();

            Object[] fila = new Object[4];
            fila[0] = nt;

                Object[] aux = (Object[])nt.getData();
                fila[1] = aux[1];
                fila[2] = aux[2];
                fila[3] = aux[3];

            modelo.addRow(fila);
        }

    }

    public void filtrarPorEmpleado(int id)
    {
        this.ID_EMPLEADO = id;
        mostrarLicencias();
        this.setTitle("Consultar Licencias - "+gestor.getNombreEmpleado(id));
        // TODOS LAS LICENCIAS MOSTRADAS SON PARA UN EMPLEADO
    }

    /**
     * EG-0013 : No se pudo cargar las licencias
     * @param cod
     */
    public void MostrarMensaje(String cod)
    {
        System.out.println("Mensaje "+cod+" detectado !!!");
        if(cod.equals("EG-0013"))
        {
            JOptionPane.showMessageDialog(this.getParent(),"No se pudo cargar la lista de Licencias","Error en la Carga",JOptionPane.ERROR_MESSAGE);
        }
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtBuscar = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblLista = new javax.swing.JTable();
        btnEliminar = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();
        btnNuevo = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Consultar Licencias");

        txtBuscar.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        txtBuscar.setForeground(new java.awt.Color(102, 102, 102));
        txtBuscar.setText("Buscar...");
        txtBuscar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtBuscarMouseClicked(evt);
            }
        });
        txtBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtBuscarActionPerformed(evt);
            }
        });
        txtBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtBuscarKeyPressed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/search.png"))); // NOI18N

        tblLista.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Legajo", "Nombre Completo", "Fecha de Licencia", "Tipo"
            }
        ));
        jScrollPane1.setViewportView(tblLista);

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/delete_page.png"))); // NOI18N
        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        btnModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/text_page.png"))); // NOI18N
        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/add_page.png"))); // NOI18N
        btnNuevo.setText("Nueva Licencia");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/search_page.png"))); // NOI18N
        jButton4.setText("Ver Detalles");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 725, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnNuevo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnModificar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnEliminar)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEliminar)
                    .addComponent(btnModificar)
                    .addComponent(btnNuevo)
                    .addComponent(jButton4))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtBuscarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtBuscarMouseClicked

        if(txtBuscar.getText().equals("Buscar..."))
        {
            txtBuscar.setText("");
        }

    }//GEN-LAST:event_txtBuscarMouseClicked

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed

        pantallaGestionLicenciasEmpleado pgle = new pantallaGestionLicenciasEmpleado();
        pgle.seleccionarEmpleadoLicencia(ID_EMPLEADO);
        SwingPanel.getInstance().addWindow(pgle);
        pgle.setCallBack(this);
        pgle.setVisible(true);

    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed

        if(tblLista.getSelectedRow()>-1)
        {
            DefaultTableModel modelo = (DefaultTableModel)tblLista.getModel();
            NTupla nt = (NTupla) modelo.getValueAt(tblLista.getSelectedRow(),0);
            int resp=JOptionPane.showConfirmDialog(this.getParent(), "Esta seguro que desea eliminar la licencia", "Eliminar licencia", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if(resp==JOptionPane.YES_OPTION)       
            {
                gestor.eliminarLicencia(nt);
                refrescarTabla();
                System.out.println("Se va a eliminar la Licencia: "+nt.getId());

            }
        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        if(tblLista.getSelectedRow()>-1)
        {
            DefaultTableModel modelo = (DefaultTableModel)tblLista.getModel();
            NTupla nt = (NTupla) modelo.getValueAt(tblLista.getSelectedRow(),0);

            // TENGO LA LICENCIA, LA MANDO A MODIFICAR
            pantallaGestionLicenciasEmpleado pgle = new pantallaGestionLicenciasEmpleado();
            SwingPanel.getInstance().addWindow(pgle);
            pgle.setCallBack(this);
            pgle.modificarLicencia(nt.getId());
            pgle.setVisible(true);

        }
    }//GEN-LAST:event_btnModificarActionPerformed

    private void txtBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtBuscarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtBuscarActionPerformed

    private void txtBuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
            JOptionPane.showMessageDialog(this.getParent(),"En Construcción","ATENCION",JOptionPane.INFORMATION_MESSAGE);

        }
    }//GEN-LAST:event_txtBuscarKeyPressed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        if(tblLista.getSelectedRow()>-1)
        {
            DefaultTableModel modelo = (DefaultTableModel)tblLista.getModel();
            NTupla nt = (NTupla) modelo.getValueAt(tblLista.getSelectedRow(),0);

            pantallaGestionLicenciasEmpleado pgle = new pantallaGestionLicenciasEmpleado();
            
            SwingPanel.getInstance().addWindow(pgle);
            pgle.setCallBack(this);
            pgle.mostrarLicencia(nt.getId());
            pgle.setVisible(true);

        }
    }//GEN-LAST:event_jButton4ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblLista;
    private javax.swing.JTextField txtBuscar;
    // End of variables declaration//GEN-END:variables

    public void actualizar(int flag, boolean exito) {       
            refrescarTabla();
    }

}
